package com.xh.baselibrary.utils.net;

import android.content.Context;

import com.xh.baselibrary.log.XLog;
import com.xh.baselibrary.utils.net.ping.IPingPolicy;

public class NetCheckManager {
    private static final String TAG = "NetCheckManager";
    private static final boolean OPEN_REQUEST_NET = false;
    private static final String BACK_HOST = "dds.";
    private volatile NetPolicy.NetState mNetState = NetPolicy.NetState.HEALTHY;
    private static final String[] TRY_HOST = {
            "www.baidu.com",
            "www.tencent.com",
            "www.tmall.com",
            "www.mi.com",
            "www.antfin.com",
            "www.toutiao.com",
            "www.163.com",
            "www.taobao.com",
            "www.jd.com",
            "www.huawei.com",
            "www.webank.com",
            "www.tmall.com"
    };
    //弱网判定延迟调整至1800
    private static final long WEAK_NET_DELAY_LIMIT =1800L;
    //定时器循环延迟1S
    private static final int SCHEDULE_TIME = 1000;
    //稳定网络判定条件
    private static final int WEAK_NET_CHECK_TIMES = 3;
    private PingWorker mPingWorker;
    private RequestNetWorker mRequestNetWorker;

    private boolean mInit = false;
    private boolean mRunning  =false;
    private static class Holder{
        public static final NetCheckManager INSTANCE= new NetCheckManager();
    }
    public static NetCheckManager getInstance(){
        return Holder.INSTANCE;
    }
    public synchronized void initAndStart(Context context){
        XLog.i(TAG,"initAndStart--->");
        init(context);
        start();
    }

    public synchronized void stopAndRelease(){
        stop();
        release();
    }
    private void init(Context context) {
        XLog.i(TAG,"init:"+mInit);
        if (mInit){
            return;
        }
        try {
            NetPolicy policy =new NetPolicy(new WeakNetPolicyListenerImpl(),WEAK_NET_CHECK_TIMES,WEAK_NET_DELAY_LIMIT);
            mPingWorker = new PingWorker(context,TRY_HOST,BACK_HOST, IPingPolicy.POLICY_SYSTEM_CMD,policy);
            if (OPEN_REQUEST_NET){
                mRequestNetWorker = new RequestNetWorker(new String[]{"http://log.aispeech.com"},policy);
            }
            mInit =true;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    void start(){
        if (mRunning){
            return;
        }
        if (mRequestNetWorker != null) {
            mRequestNetWorker.start();
        }
        if (mPingWorker != null && !mPingWorker.isWorking()) {
            mPingWorker.start();
        }
        mRunning = true;
    }

    void stop(){
        if (!mRunning){
            return;
        }
        if (mRequestNetWorker != null) {
            mRequestNetWorker.stop();
        }
        if (mPingWorker != null && mPingWorker.isWorking()) {
            mPingWorker.start();
        }
        mRunning = false;
    }
    void release(){
        if (!mInit){
            return;
        }
        if (mPingWorker != null) {
            mPingWorker.release();
        }
        mPingWorker = null;
        mRequestNetWorker = null;
        mRunning = false;
        mInit = false;
    }

    public boolean isNetWorkOk(){
        return mNetState != NetPolicy.NetState.WEAK;
    }

    private class WeakNetPolicyListenerImpl implements NetPolicy.IWeakNetPolicyListener{

        @Override
        public void onNetStateChange(NetPolicy.NetState netState) {
            switch (netState){
                case WEAK:
                    XLog.i(TAG,"");
                    if (mPingWorker != null && mPingWorker.isWorking()){
                        mPingWorker.setScheduleTime(SCHEDULE_TIME);
                    }
                    mNetState = NetPolicy.NetState.WEAK;
                    break;
                case HEALTHY:
                    mNetState = NetPolicy.NetState.WEAK;
                    break;
                case STRONG:
                    mNetState = NetPolicy.NetState.STRONG;
                    if (mPingWorker != null && mPingWorker.isWorking()){
                        //网络稳定的时候，可以改为3倍间隔去检测一次
                        mPingWorker.setScheduleTime(SCHEDULE_TIME * 3);
                    }
                    break;
            }
        }
    }
}
